<?php

namespace app\admin\service;

use app\admin\model\BoxGame;
use app\admin\model\BoxGameOpen;
use app\admin\model\BoxRebate;
use app\admin\model\BoxRebateLog;
use app\admin\model\Member;
use think\Db;

class BoxRebateService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new BoxRebate();
    }

    public function getList()
    {
        $param = request()->param();
        // 查询条件
        $map = [];

        if (isset($param['game_id']) && !empty($param['game_id'])) {
            $map[] = ['game_id', '=', $param['game_id']];
        }

        if (isset($param['open_id']) && !empty($param['open_id'])) {
            $map[] = ['open_id', '=', $param['open_id']];
        }

        if (!empty($param['time_range'][0]) && !empty($param['time_range'][1])) {
            $map[] = ['create_time', '>=', $param['time_range'][0]];
            $map[] = ['create_time', '<=', $param['time_range'][1]];
        }
        $data = $this->model->where($map)->order('id', 'desc')->page(PAGE, PERPAGE)->select();

        if (!empty($data)) {
            $gameData = BoxGame::field(['id', 'game_name'])->select()->toArray();
            $gameData = array_column($gameData, 'game_name', 'id');

            $openData = BoxGameOpen::field(['id', 'server_id', 'name'])->select()->toArray();
            $openData = array_column($openData, 'name', 'id');

            foreach ($data as &$val) {
                $val['game_name']      = $gameData[$val['game_id']] ?? '';
                $val['open_name']      = $openData[$val['open_id']] ?? '';
                $val['send_type_name'] = BoxRebate::$sent_type[$val['send_type']] ?? '';
                if (!empty($val['gradient'])) {
                    $gradient     = json_decode($val['gradient'], true);
                    $gradientData = '';
                    foreach ($gradient as $item) {
                        $gradientData .= ' 充 ' . $item['item'] . ' 返 ' . $item['value'] . ' %; ';
                    }
                    $val['gradient_data'] = $gradientData;
                    $val['gradient']      = json_decode($val['gradient']);
                }
            }

        }
        //获取数据总数
        $count = $this->model->where($map)->count();

        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $data,
            "count" => $count,
        ];
    }

    public function edit()
    {
        $param = request()->param();
        if (empty($param['game_id'])) {
            return message("请选择游戏", false);
        }

        if (isset($param['id']) && !empty($param['id'])) {
            $this->model->update($param, ['id' => $param['id']]);
        } else {
            $param['create_time'] = date('Y-m-d H:i:s');
            $this->model->save($param);
        }

        return message();
    }


    public function status()
    {
        $param = request()->param();
        if (!$param['id']) {
            return message('记录ID不能为空', false);
        }
        if (!isset($param['status'])) {
            return message('记录状态不能为空', false);
        }
        $this->model->edit($param);
        return message();
    }


    /**
     * 获取所有游戏列表
     * @return array
     */
    public function getGameList()
    {
        $map[]    = ['status', '=', 1];
        $map[]    = ['mark', '=', 1];
        $map[]    = ['type', '=', 1];
        $openList = BoxGameOpen::where($map)->select()->toArray();
        $gameIds  = array_column($openList, 'game_id');

        $gameList = BoxGame::field(['id', 'game_name'])->whereIn('id', $gameIds)->order("id DESC")->select();
        if (!empty($gameList)) {
            foreach ($gameList as &$item) {
                $item['game_name'] = $item['id'] . '/' . $item['game_name'];
            }
        }
        return message("操作成功", true, $gameList);
    }

    /**
     * 获取游戏对应区服列表
     * @return array
     */
    public function getGameOpenList()
    {
        $param = request()->param();
        $map   = [];
        if (isset($param['game_id']) && !empty($param['game_id'])) {
            $map[] = ['game_id', '=', $param['game_id']];
        }
        $map[]    = ['status', '=', 1];
        $map[]    = ['mark', '=', 1];
        $map[]    = ['type', '=', 1];
        $openList = BoxGameOpen::field(['id', 'server_id', 'name'])->where($map)->order('open_time', 'desc')->select();
        if (!empty($openList)) {
            foreach ($openList as &$item) {
                $item['name'] = $item['id'] . '/' . $item['name'];
            }
        }

        return message("操作成功", true, $openList);
    }

    /**
     * 获取充值方式
     * @return array
     */
    public function getOpenType()
    {
        return message("操作成功", true, BoxRebate::$sent_type);
    }


    /**
     * @Notes:返利记录
     * @Interface rebatelog
     * @return array
     */
    public function rebatelog()
    {
        $param = request()->param();
        // 查询条件
        $map = [];

        if (isset($param['box_game_id']) && !empty($param['box_game_id'])) {
            $map[] = ['box_game_id', '=', $param['box_game_id']];
        }

        if (isset($param['open_id']) && !empty($param['open_id'])) {
            $map[] = ['open_id', '=', $param['open_id']];
        }

        if (!empty($param['time_range'][0]) && !empty($param['time_range'][1])) {
            $map[] = ['create_time', '>=', $param['time_range'][0]];
            $map[] = ['create_time', '<=', $param['time_range'][1]];
        }
        $data = BoxRebateLog::where($map)->order('id', 'desc')->page(PAGE, PERPAGE)->select();

        if (!empty($data)) {
            $platData = Db::name('user_channel')->field(['id', 'nickname'])->select();
            $platData = array_column($platData, 'nickname', 'id');

            $gameData = BoxGame::field(['id', 'game_name'])->select()->toArray();
            $gameData = array_column($gameData, 'game_name', 'id');

            $openData = BoxGameOpen::field(['id', 'server_id', 'name'])->select()->toArray();
            $openData = array_column($openData, 'name', 'id');

            $memberIds  = array_column($data->toArray(), 'member_id');
            $memberInfo = (new Member())->field('id,nickname')->whereIn('id', $memberIds)->select()->toArray();
            $memberInfo = array_column($memberInfo, 'nickname', 'id');

            foreach ($data as &$val) {
                $val['plat_name']      = $platData[$val['plat_id']] ?? '';
                $val['game_name']      = $gameData[$val['box_game_id']] ?? '';
                $val['open_name']      = $openData[$val['open_id']] ?? '';
                $val['send_type_name'] = BoxRebate::$sent_type[$val['send_type']] ?? '';
                if (!empty($val['gradient'])) {
                    $gradient     = json_decode($val['gradient'], true);
                    $gradientData = '';
                    foreach ($gradient as $item) {
                        $gradientData .= ' 充 ' . $item['item'] . ' 返 ' . $item['value'] . ' %; ';
                    }
                    $val['gradient_data'] = $gradientData;
                    $val['gradient']      = json_decode($val['gradient']);
                }
                $val['member_name'] = $memberInfo[$val['member_id']] ?? '';

            }

        }
        //获取数据总数
        $count = BoxRebateLog::where($map)->count();

        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $data,
            "count" => $count,
        ];
    }

}
